﻿using Weather3.Data;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using System.Net.Http;
using System.Text;
using System.Xml.Linq;
using Windows.UI.Xaml.Media.Imaging;
using System.Net.Http.Headers;
using Windows.Networking.Connectivity;
using Windows.UI.Popups;
using System.Threading.Tasks;
using Windows.Data.Xml.Dom;
using System.Diagnostics;
using Windows.UI.Notifications;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage.Streams;
using Windows.Graphics.Imaging;


// The Group Detail Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234229

namespace Weather3
{
    /// <summary>
    /// A page that displays an overview of a single group, including a preview of the items
    /// within the group.
    /// </summary>
    public sealed partial class GroupDetailPage : Weather3.Common.LayoutAwarePage
    {
       
        private  string city = "北京";

        Weather weather = (Application.Current as App).weather;

        //private string dataPackageTitle = "看天气";
        //private string dataPackageDescription = "";
        ////private RandomAccessStreamReference dataPackageThumbnail = null;
        //private string dataPackageText = "";


        public GroupDetailPage()
        {
            this.InitializeComponent();
            this.Loaded += GroupDetailPage_Loaded;
           // ShareSourceLoad();
            //DayWeather(city);
            
        }



        public void ShareSourceLoad()
        {
            DataTransferManager datatransferManager;
            datatransferManager = DataTransferManager.GetForCurrentView();
            datatransferManager.DataRequested += new TypedEventHandler<DataTransferManager, DataRequestedEventArgs>(this.DataRequested);
        }

        //共享按钮
        async void DataRequested(DataTransferManager sender, DataRequestedEventArgs e)
        {
            if (weather != null)
            {
                e.Request.Data.Properties.Title = weather.city; ;
                e.Request.Data.Properties.Description = weather.temperature1;
                //if (this.dataPackageThumbnail != null)
                //{
                //    e.Request.Data.Properties.Thumbnail = this.dataPackageThumbnail;
                //}
                e.Request.Data.SetText(weather.temperature1 + weather.temperature2 + weather.chy_shuoming);
            }
        }

        async void GroupDetailPage_Loaded(object sender, RoutedEventArgs e)
        {
            
            //TODO检查网络
            string connectionProfileInfo = string.Empty;
            ConnectionProfile InternetConnectionProfile = NetworkInformation.GetInternetConnectionProfile();

            if (InternetConnectionProfile == null)
            {
                day0Weather.Visibility = Visibility.Collapsed;
                day1Weather.Visibility = Visibility.Collapsed;
                day2Weather.Visibility = Visibility.Collapsed;
                day3Weather.Visibility = Visibility.Collapsed;

                await new MessageDialog("请确保设备已经连接到网络！", "网络异常").ShowAsync();
                 
            }
            else
            {
                day0Weather.Visibility = Visibility.Visible;
                day1Weather.Visibility = Visibility.Visible;
                day2Weather.Visibility = Visibility.Visible;
                day3Weather.Visibility = Visibility.Visible;

                DayWeather(city);
                //pageTitle.Text = city;      
                //Titles();
            }
        }


        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property provides the group to be displayed.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            
            if ((Application.Current as App).City != null)
            {
                city = (Application.Current as App).City;
            }
            pageTitle.Text = city; 
        }

        async void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            //PopupInputControl inputDialog = new PopupInputControl();
            //Popup popup = new Popup
            //{
            //    Child = inputDialog,
            //    IsLightDismissEnabled = true
            //};
            //inputDialog.OnValueChanged += (dialogSender, dialogArgs) =>
            //{
            //    Debug.WriteLine("输出inputDialog.InputString：" + inputDialog.InputString);

            //   city = inputDialog.InputString;

            //    popup.IsOpen = false;
            //    if (!string.IsNullOrEmpty(city))
            //    {
            //       pageTitle.Text = city;
            //      DayWeather(city);
                   
            //    }
            //};
            //inputDialog.SizeChanged += (dialogSender, dialogArgs) =>
            //{
            //    popup.VerticalOffset = this.ActualHeight - inputDialog.ActualHeight - this.BottomAppBar.ActualHeight- 20;
            //    popup.HorizontalOffset = this.ActualWidth - 400;
            //};
            //popup.IsOpen = true;


            Titles();
        }

        private void DayWeather(string str)
        {
            day0Weather.City = str;
            day1Weather.City = str;
            day2Weather.City = str;
            day3Weather.City = str;
            //this.dataPackageTitle = day0Weather.dataPackageTitle;
            // this.dataPackageDescription =day0Weather.dataPackageDescription;
            // this.dataPackageText = day0Weather.dataPackageText;
            
        }
        
        

        /// <summary>
        /// Live Titles 动态瓷贴
        /// </summary>
        private  void Titles()
        {
            XmlDocument titlexml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWideSmallImageAndText02);
            XmlNodeList titleTextAttributes = titlexml.GetElementsByTagName("text");

            for (int i = 0; i < titleTextAttributes.Length; i++)
            {
                string titleText = String.Empty;
                titleText = "Text text" + i;
                titleTextAttributes.Item((uint)i).AppendChild(titlexml.CreateTextNode(titleText));

            }
            // XmlDocument titlexml1 = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWideImage);
            XmlNodeList imageElements = titlexml.GetElementsByTagName("image");
            for (int i = 0; i < imageElements.Length; i++)
            {
                XmlElement imageElement = (XmlElement)imageElements.Item((uint)i);
                string imageSource = String.Empty;
                imageSource = "ms-appx:///Images/Logo.png";
                imageElement.SetAttribute("src", imageSource);
            }

            XmlElement bingdingElement = (XmlElement)titlexml.GetElementsByTagName("binding").Item(0);
            bingdingElement.SetAttribute("branding", "None");

            TileNotification tileNotification = new TileNotification(titlexml);


            // TileNotification tileNotification1 = new TileNotification(titlexml1);
            //tileNotification.ExpirationTime = DateTimeOffset.UtcNow.AddSeconds(5);
            TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotification);
            // TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotification1);
            //XmlDocument titlexml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileWideText07);
            //XmlNodeList titleTextAttributes = titlexml.GetElementsByTagName("text");
            //for (int i = 0; i < titleTextAttributes.Length; i++)
            //{
            //    string titleText = String.Empty;
            //    titleText = "Text text" + i;
            //    titleTextAttributes.Item((uint)i).AppendChild(titlexml.CreateTextNode(titleText));

            //}
            ////if (weather != null)
            ////{
            ////    titleTextAttributes.Item((uint)0).AppendChild(titlexml.CreateTextNode(weather.city));
            ////    titleTextAttributes.Item((uint)1).AppendChild(titlexml.CreateTextNode("白天" + weather.temperature1 + "℃"));
            ////    titleTextAttributes.Item((uint)2).AppendChild(titlexml.CreateTextNode("夜间" + weather.temperature2 + "℃"));
            ////    titleTextAttributes.Item((uint)3).AppendChild(titlexml.CreateTextNode(weather.gm_l));
            ////    titleTextAttributes.Item((uint)4).AppendChild(titlexml.CreateTextNode(weather.gm_s));
            ////}
            ////XmlNodeList imageElements = titlexml.GetElementsByTagName("image");
            ////XmlElement imageElement = (XmlElement)imageElements.Item((uint)0);
            ////string imageSoure = "ms-appx:///Images/tile-sdk.png";
            ////imageElement.SetAttribute("src", imageSoure);
            
            //XmlElement bingdingElement = (XmlElement)titlexml.GetElementsByTagName("binding").Item(0);
            //bingdingElement.SetAttribute("branding", "None");

            //TileNotification tileNotification = new TileNotification(titlexml);
            //TileUpdateManager.CreateTileUpdaterForApplication().Update(tileNotification);
        }

        private void btnCity_Click_1(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(CityPage));
        }

    }

}
